package cn.deschen.app.controller;

import cn.deschen.app.vo.UnitDetailVO;
import cn.deschen.app.vo.UnitPageVO;
import cn.deschen.biz.domain.entity.AdPlan;
import cn.deschen.biz.domain.entity.AdUnit;
import cn.deschen.biz.dto.unit.*;
import cn.deschen.biz.service.AdPlanService;
import cn.deschen.biz.service.AdUnitEleService;
import cn.deschen.biz.service.AdUnitService;
import cn.deschen.common.api.CmPage;
import cn.deschen.common.api.CmRes;
import cn.deschen.common.api.CmResCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author hanbin_chen
 * @Description 推广单元控制层
 * @DateTime 2021/06/21
 * @Version V1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/ad/unit")
@Api(tags = "推广单元管理")
public class AdUnitController {

    @Autowired
    private AdPlanService planService;

    @Autowired
    private AdUnitService unitService;

    @Autowired
    private AdUnitEleService unitEleService;

    @Autowired
    private MapperFacade mapperFacade;

    @ApiOperation("创建推广单元")
    @PostMapping()
    public CmRes<Void> add(@Valid @RequestBody UnitAddDTO addDTO) {
        boolean planIfExisted = checkPlanIfExisted(addDTO.getPlanId());
        if (!planIfExisted) {
            return CmRes.failure(CmResCode.DATA_NOT_FOUND.fillArgs("推广计划"));
        }

        boolean ifExisted = checkIfExisted(addDTO.getPlanId(), addDTO.getUnitName());
        if (ifExisted) {
            return CmRes.failure(CmResCode.REPEAT_OPERATION.extra("该推广单元已存在"));
        }
        unitService.add(addDTO);
        return CmRes.success();
    }

    private boolean checkPlanIfExisted(Long planId) {
        AdPlan plan = planService.getById(planId);
        if (null != plan) {
            return true;
        }
        return false;
    }

    private boolean checkIfExisted(Long planId, String unitName) {
        QueryWrapper<AdUnit> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AdUnit::getPlanId, planId)
                .eq(AdUnit::getUnitName, unitName);
        AdUnit unit = unitService.getOne(wrapper);
        if (null != unit) {
            return true;
        }
        return false;
    }

    @ApiOperation("更新推广单元")
    @PutMapping()
    public CmRes<Void> update(@Valid @RequestBody UnitUpdateDTO updateDTO) {
        AdUnit oldUnit = unitService.getById(updateDTO.getId());
        if (null == oldUnit) {
            return CmRes.failure(CmResCode.DATA_NOT_FOUND.fillArgs("推广单元"));
        }
        if (!oldUnit.getPlanId().equals(updateDTO.getPlanId())
                || !oldUnit.getUnitName().equals(updateDTO.getUnitName())) {
            boolean ifExisted = checkIfExisted(updateDTO.getPlanId(), updateDTO.getUnitName());
            if (ifExisted) {
                return CmRes.failure(CmResCode.REPEAT_OPERATION.fillArgs("该推广单元已存在"));
            }
        }
        unitService.modify(updateDTO);

        return CmRes.success();
    }

    @ApiOperation("根据主键获取兴趣推广单元详情")
    @GetMapping("/detail")
    public CmRes<UnitDetailVO> detail(@ApiParam("推广单元ID") @RequestParam Long id) {
        AdUnit unit = unitService.getById(id);
        Map<String, List<UnitEleDTO>> eleMap = unitEleService.getElesById(id);
        UnitDetailVO detailVO = mapperFacade.map(unit, UnitDetailVO.class);
        detailVO.setEleMap(eleMap);
        return CmRes.success(detailVO);
    }

    @ApiOperation("删除推广单元")
    @DeleteMapping()
    public CmRes<Void> delete(@RequestParam List<Long> ids) {
        unitService.removeBatchByIds(ids);
        return CmRes.success();
    }

    @ApiOperation("获取推广单元列表")
    @GetMapping("/page")
    public CmRes<CmPage<UnitPageVO>> page(UnitQueryDTO queryDTO,
                                          @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        List<UnitPageDTO> pageDTOList = unitService.listByQueryDTO(queryDTO);
        Page<UnitPageVO> pageVO = new Page<>();
        BeanUtils.copyProperties(pageDTOList, pageVO);
        List<UnitPageVO> records = mapperFacade.mapAsList(pageDTOList, UnitPageVO.class);
        pageVO.addAll(records);
        return CmRes.success(CmPage.restPage(pageVO));
    }

}


